import { createAsyncThunk, createSlice } from '@reduxjs/toolkit'
import { getDoctorListApi } from '../api'

export const fetchGetDoctorList = createAsyncThunk(
  'createOrder/fetchGetDoctorList',
  async () => {
    const result = await getDoctorListApi()
    console.log(result)
    return result.data
  }
)
// interface info {
//   hospital_id: number
//   hospital_name: string
//   companion_id: number
//   receiveAddress: string
//   tel: string
//   demand: string
//   starttime: number
// }

interface initial {
  companion: any[]
  hospitals: any[]
  hospitalList: any[]
  service?: any
}
const initialState: initial = {
  companion: [],
  hospitals: [],
  hospitalList: []
}
const createOrderSlice = createSlice({
  name: 'createOrder',
  initialState: initialState,
  reducers: {},
  extraReducers: (builder) => {
    builder.addCase(fetchGetDoctorList.fulfilled, (state, { payload }) => {
      console.log('✅ reducer触发成功，数据：', payload)
      const { companion, hospitals } = payload
      state.companion = [
        companion.map((item: any) => ({
          label: `${item.name}（${item.mobile}）`,
          value: item.name,
          key: item.id
        }))
      ]

      state.hospitals = [
        hospitals.map((item: any) => ({
          label: item.name,
          value: item.name,
          key: item.id
        }))
      ]
      state.hospitalList = hospitals
    })
  }
})

export default createOrderSlice.reducer
